package com.hr.checkrepetition.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: check-repetition
 * @description:
 * @author: hjk
 * @create: 2021-07-08 18:21
 **/
@Configuration
public class DelayedQueueConfig {

    /**
     * 延时队列
     */
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";
    /**
     * 延时交换机
     */
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    /**
     * 延时 routing_key
     */
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    /**
     * 申明延时队列
     * @return
     */
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    /**
     *  自定义交换机 申明是一个延迟交换机
     * @return
     */
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        //自定义交换机的类型
        args.put("x-delayed-type", ExchangeTypes.DIRECT);
        return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    /**
     * 延迟交换机绑定延时队列
     * @param queue
     * @param delayedExchange
     * @return
     */
    @Bean
    public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue,
                                       @Qualifier("delayedExchange") CustomExchange delayedExchange) {
        return BindingBuilder.bind(queue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }

    /**
     *  申明一个交换机（测试发布确认）
     */
    public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";
    /**
     *  申明一个队列（测试发布确认）
     */
    public static final String CONFIRM_QUEUE_NAME = "confirm.queue";

    /**
     * 申请交换机
     * @return
     */
   /* @Bean
    public DirectExchange confirmExchange(){
        return new DirectExchange(CONFIRM_EXCHANGE_NAME);
    }*/

    /**
     * 申明队列
     * @return
     */
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }

    /**
     * 声明确认队列绑定关系
     * @param confirmQueue
     * @param confirmExchange
     * @return
     */
    @Bean
    public Binding queueBinding(Queue confirmQueue,DirectExchange confirmExchange){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with("confirm_routing_key");
    }

    /**
     *  备份交换机
     */
    public static final String BACKUP_EXCHANGE_NAME = "backup.exchange";
    /**
     * 备份队列
     */
    public static final String BACKUP_QUEUE_NAME = "backup.queue";
    /**
     *  报警队列
     */
    public static final String WARNING_QUEUE_NAME = "warning.queue";

    /**
     *  申明备份 交换机
     * @return
     */
    @Bean
    public FanoutExchange backupExchange(){
        return ExchangeBuilder.fanoutExchange(BACKUP_EXCHANGE_NAME).durable(true).build();
        //return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    /**
     *  声明备份队列
     * @return
     */
    @Bean
    public Queue backQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    /**
     *  声明备份队列绑定关系
     * @param backQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding backupBinding(Queue backQueue, FanoutExchange backupExchange){
        return BindingBuilder.bind(backQueue).to(backupExchange);
    }

    //声明确认 Exchange 交换机的备份交换机
    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        ExchangeBuilder exchangeBuilder =
                ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME)
                        .durable(true)
                        //设置该交换机的备份交换机
                        .withArgument("alternate-exchange", BACKUP_EXCHANGE_NAME);
        return (DirectExchange)exchangeBuilder.build();
    }

    /**
     *  声明警告队列
     * @return
     */
    @Bean
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }

    /**
     * 声明报警队列绑定关系
     * @param warningQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding warningBinding(Queue warningQueue, FanoutExchange backupExchange){
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }
}
